package com.sky.jsoup.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Set;

public class JsoupMain {

    public static void main(String[] args) throws Exception{
        getSS();
        //getSFP();
        //getZJQ();
        //getBQCSF();
        //getBFS();
        //getDuiZhenShuJu("11833", "2020-07-10", 10);
        //getHistoryData("125388", 10);
        //getKJjieguo("125371");
    }

    /**
     * 获取开奖玩法
     * @param mid
     * @throws Exception
     */
    public static void getKJjieguo(String mid)throws Exception{
        String res = HttpUtil.get("https://i.sporttery.cn/api/fb_match_info/get_pool_rs/?f_callback=pool_prcess&mid="+mid+"&_=1594369811001");
        if (StrUtil.isNotBlank(res)) {
            res = res.replaceAll("pool_prcess\\(", "");
            res = res.replaceAll("\\);", "");
            JSONObject resj = JSONObject.parseObject(res);
            String result = resj.getString("result");
            JSONArray array = new JSONArray();
            if (StrUtil.isNotBlank(result)) {
                JSONObject resultj = JSONObject.parseObject(result);
                String poolRs = resultj.getString("pool_rs");
                if (StrUtil.isNotBlank(poolRs)) {
                    JSONObject poolRsj = JSONObject.parseObject(poolRs);
                    String crs = poolRsj.getString("crs"); // 获取比分玩法
                    if (StrUtil.isNotBlank(crs)) {
                        JSONObject wf = new JSONObject();
                        JSONObject crsj = JSONObject.parseObject(crs);
                        wf.put("玩法名称", "比分");
                        wf.put("开奖结果", crsj.getString("prs_name"));
                        wf.put("奖金", crsj.getString("odds"));
                        array.add(wf);
                    }
                    String had = poolRsj.getString("had"); // 胜平负
                    if (StrUtil.isNotBlank(had)) {
                        JSONObject wf = new JSONObject();
                        JSONObject hadj = JSONObject.parseObject(had);
                        wf.put("玩法名称", "胜平负");
                        wf.put("开奖结果", hadj.getString("prs_name"));
                        wf.put("奖金", hadj.getString("odds"));
                        array.add(wf);
                    }
                    String hafu = poolRsj.getString("hafu");
                    if (StrUtil.isNotBlank(hafu)) {
                        JSONObject wf = new JSONObject();
                        JSONObject hafuj = JSONObject.parseObject(hafu);
                        wf.put("玩法名称", "半全场胜平负");
                        wf.put("开奖结果", hafuj.getString("prs_name"));
                        wf.put("奖金", hafuj.getString("odds"));
                        array.add(wf);
                    }
                    String hhad = poolRsj.getString("hhad");
                    if (StrUtil.isNotBlank(hhad)) {
                        JSONObject wf = new JSONObject();
                        JSONObject hhadj = JSONObject.parseObject(hhad);
                        wf.put("玩法名称", "让球胜平负");
                        wf.put("开奖结果", "("+ hhadj.getString("goalline") +")"+ hhadj.getString("prs_name"));
                        wf.put("奖金", hhadj.getString("odds"));
                        array.add(wf);
                    }
                    String ttg = poolRsj.getString("ttg");//总进球
                    if (StrUtil.isNotBlank(ttg)) {
                        JSONObject wf = new JSONObject();
                        JSONObject hhadj = JSONObject.parseObject(ttg);
                        wf.put("玩法名称", "总进球");
                        wf.put("开奖结果", hhadj.getString("prs_name"));
                        wf.put("奖金", hhadj.getString("odds"));
                        array.add(wf);
                    }
                }
            }
            System.out.println(JSONObject.toJSONString(array));
        }
    }

    /**
     * 比分数
     * @throws Exception
     */
    public static void getBFS() throws Exception{
        String result = HttpUtil.get("https://i.sporttery.cn/odds_calculator/get_odds?i_format=json&i_callback=getData&poolcode[]=crs&_=" + DateUtil.date().getTime());
        if (StrUtil.isNotBlank(result)) {
            result = result.replaceAll("getData\\(", "");
            result = result.replaceAll("\\);", "");
        }
        JSONObject json = JSONObject.parseObject(result);
        json.remove("status");
        String datas = json.getString("data");
        JSONObject j = JSONObject.parseObject(datas);
        Set<String> s = j.keySet();
        JSONArray array = new JSONArray();
        for (String key : s) {
            JSONObject nr = new JSONObject();
            String values = j.getString(key);
            JSONObject vj = JSONObject.parseObject(values);
            nr.put("赛事编号", vj.getString("num"));
            nr.put("赛事", vj.getString("l_cn_abbr"));
            nr.put("本场比赛ID", vj.getString("id"));
            nr.put("开赛时间", vj.getString("date") + " " + vj.getString("time"));
            nr.put("客队", vj.getString("a_cn_abbr"));
            nr.put("主队", vj.getString("h_cn"));
            nr.put("主队ID", vj.getString("h_id_dc"));
            nr.put("客队ID", vj.getString("a_id_dc"));
            nr.put("销售状态", vj.getString("status"));
            String crs = vj.getString("crs");
            if (StrUtil.isNotBlank(crs)) {
                JSONObject bf = new JSONObject();
                JSONObject cr = JSONObject.parseObject(crs);
                bf.put("胜其他", cr.getString("-1-h"));
                bf.put("胜1:0", cr.getString("0100"));
                bf.put("胜2:0", cr.getString("0200"));
                bf.put("胜2:1", cr.getString("0201"));
                bf.put("胜3:0", cr.getString("0300"));
                bf.put("胜3:1", cr.getString("0301"));
                bf.put("胜3:2", cr.getString("0302"));
                bf.put("胜4:0", cr.getString("0400"));
                bf.put("胜4:1", cr.getString("0401"));
                bf.put("胜4:2", cr.getString("0402"));
                bf.put("胜4:3", cr.getString("0403"));
                bf.put("胜5:0", cr.getString("0500"));
                bf.put("胜5:1", cr.getString("0501"));
                bf.put("胜5:2", cr.getString("0502"));
                bf.put("平其他", cr.getString("-1-d"));
                bf.put("平0:0", cr.getString("0000"));
                bf.put("平1:1", cr.getString("0101"));
                bf.put("平2:2", cr.getString("0202"));
                bf.put("平3:3", cr.getString("0303"));
                bf.put("负其他", cr.getString("-1-a"));
                bf.put("负0:1", cr.getString("0001"));
                bf.put("负0:2", cr.getString("0002"));
                bf.put("负1:2", cr.getString("0102"));
                bf.put("负0:3", cr.getString("0003"));
                bf.put("负1:3", cr.getString("0103"));
                bf.put("负2:3", cr.getString("0203"));
                bf.put("负0:4", cr.getString("0004"));
                bf.put("负1:4", cr.getString("0104"));
                bf.put("负2:4", cr.getString("0204"));
                bf.put("负0:5", cr.getString("0005"));
                bf.put("负1:5", cr.getString("0105"));
                bf.put("负2:5", cr.getString("0205"));
                nr.put("赔率", bf.toJSONString());
            }
            array.add(nr);
        }
        System.out.println(JSONObject.toJSONString(array));
    }

    /**
     * 赛事
     * @throws Exception
     */
    public static void getSS() throws Exception{
        Document document = Jsoup.connect("https://info.sporttery.cn/football/history/data_center.php").get();
        Elements elements = document.getElementsByClass("match-info").get(0).getElementsByClass("matchList");
        JSONArray array = new JSONArray();
        for (int i = 0; i < elements.size(); i++) {
            JSONObject json = new JSONObject();
            Element element = elements.get(i);
            String addr = element.attr("num");
            json.put("dq", addr);
            System.out.println("地区 === " + addr);
            String name = element.select("div.match-name div").html();
            System.out.println("国家名称 === " + name);
            json.put("gjmc", name);
            String res = "";//TransApi.buildParams(name, "auto", "en");
            JSONObject enjson = JSONObject.parseObject(res);
            if (StrUtil.isBlank(enjson.getString("error_code"))) {
                JSONArray transResult = JSONObject.parseArray(enjson.getString("trans_result"));
                json.put("enName", transResult.getJSONObject(0).getString("dst"));
            }
            String query;
            if (addr.equals("gj")) {
                query = "div.match-logo1 a";
            } else {
                query = "div.match-logo a";
            }
            Elements diva = element.select(query);
            String href = diva.attr("href");
            if (StrUtil.isNotBlank(href)) {
                String arr[] = href.split("\\=");
                json.put("gjId", arr[1]);
                System.out.println("国家ID === " + arr[1]);
            } else {
                json.put("gjId", "");
            }

            String src = diva.get(0).getElementsByTag("img").attr("src");
            if (StrUtil.isNotBlank(src)) {
                src = "http:" + src;
                URL url = new URL(src);
                String imgName = src.substring(src.lastIndexOf("."), src.length());
                URLConnection conn = url.openConnection();
                InputStream inStream = conn.getInputStream();
                String imgAddr = "/Volumes/Andy/img/" + name+imgName;
                FileOutputStream fs = new FileOutputStream(imgAddr);
                byte[] buffer = new byte[1204];
                int bytesum = 0;
                int byteread = 0;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    fs.write(buffer, 0, byteread);
                }
                json.put("imgAddr", imgAddr);
            }

            String boxId;
            if (i < 9) {
                boxId = "0" + (i+1);
            } else {
                boxId = ""+(i+1);
            }
            Elements uls = element.getElementById("box"+boxId).getElementsByTag("ul");
            for (int u = 0; u < uls.size(); u++) {
                JSONArray js = new JSONArray();
                Element li = uls.get(u);
                Elements a = li.getElementsByTag("a");
                for (int ai = 0; ai < a.size(); ai++) {
                    JSONObject ss = new JSONObject();
                    String href1 = a.get(ai).attr("href");
                    if (StrUtil.isNotBlank(href1)) {
                        String arr[] = href1.split("\\=");
                        ss.put("ssId", arr[1]);
                        System.out.println("赛事ID === " + arr[1]);
                    }
                    String ssname = a.get(ai).html();
                    ss.put("ssname", ssname);
                    String resSName = "";//TransApi.buildParams(ssname, "auto", "en");
                    JSONObject sEnName = JSONObject.parseObject(resSName);
                    if (StrUtil.isBlank(sEnName.getString("error_code"))) {
                        JSONArray transResult = JSONObject.parseArray(sEnName.getString("trans_result"));
                        ss.put("sEnName", transResult.getJSONObject(0).getString("dst"));
                    }
                    js.add(ss);
                }
                json.put("ssarr", js.toJSONString());
            }
            array.add(json);
        }
        System.out.println(JSONObject.toJSONString(array));
    }

    /**
     * 胜负平
     * @throws Exception
     */
    public static void getSFP() throws Exception {

        String result = HttpUtil.get("https://i.sporttery.cn/odds_calculator/get_odds?i_format=json&i_callback=getData&poolcode[]=hhad&poolcode[]=had&_=" + DateUtil.date().getTime());
        if (StrUtil.isNotBlank(result)) {
            result = result.replaceAll("getData\\(", "");
            result = result.replaceAll("\\);", "");
        }
        JSONObject json = JSONObject.parseObject(result);
        json.remove("status");
        String datas = json.getString("data");
        JSONObject j = JSONObject.parseObject(datas);
        Set<String> s = j.keySet();
        JSONArray array = new JSONArray();
        for (String key : s) {
            JSONObject nr = new JSONObject();
            String values = j.getString(key);
            JSONObject vj = JSONObject.parseObject(values);
            nr.put("赛事编号", vj.getString("num"));
            nr.put("赛事", vj.getString("l_cn_abbr"));
            nr.put("本场比赛ID", vj.getString("id"));
            nr.put("开赛时间", vj.getString("date") + " " + vj.getString("time"));
            nr.put("客队", vj.getString("a_cn_abbr"));
            nr.put("主队", vj.getString("h_cn"));
            nr.put("主队ID", vj.getString("h_id_dc"));
            nr.put("客队ID", vj.getString("a_id_dc"));
            nr.put("销售状态", vj.getString("status"));
            String sfppl = vj.getString("had");
            if (StrUtil.isNotBlank(sfppl)) {
                JSONObject sfpj = JSONObject.parseObject(sfppl);
                JSONObject sfp = new JSONObject();
                sfp.put("胜", sfpj.getString("h"));
                sfp.put("平", sfpj.getString("d"));
                sfp.put("负", sfpj.getString("a"));
                sfp.put("是否单", sfpj.getString("single"));
                sfp.put("赔率状态", sfpj.getString("p_status"));
                nr.put("胜负平赔率", sfp.toJSONString());
            }
            String rqsfppl = vj.getString("hhad");
            if (StrUtil.isNotBlank(rqsfppl)) {
                JSONObject rqj = JSONObject.parseObject(rqsfppl);
                JSONObject rqsfp = new JSONObject();
                rqsfp.put("胜", rqj.getString("h"));
                rqsfp.put("平", rqj.getString("d"));
                rqsfp.put("负", rqj.getString("a"));
                rqsfp.put("赔率状态", rqj.getString("p_status"));
                rqsfp.put("让球数", rqj.getString("fixedodds"));
                nr.put("让球胜负平赔率", rqsfp.toJSONString());
            }
            array.add(nr);
        }
        System.out.println(JSONObject.toJSONString(array));
    }

    /**
     * 总进球谁
     * @throws Exception
     */
    public static void getZJQ() throws Exception {
        String result = HttpUtil.get("https://i.sporttery.cn/odds_calculator/get_odds?i_format=json&i_callback=getData&poolcode[]=ttg&_=" + DateUtil.date().getTime());
        if (StrUtil.isNotBlank(result)) {
            result = result.replaceAll("getData\\(", "");
            result = result.replaceAll("\\);", "");
        }
        JSONObject json = JSONObject.parseObject(result);
        json.remove("status");
        String datas = json.getString("data");
        JSONObject j = JSONObject.parseObject(datas);
        Set<String> s = j.keySet();
        JSONArray array = new JSONArray();
        for (String key : s) {
            JSONObject nr = new JSONObject();
            String values = j.getString(key);
            JSONObject vj = JSONObject.parseObject(values);
            nr.put("赛事编号", vj.getString("num"));
            nr.put("赛事", vj.getString("l_cn_abbr"));
            nr.put("本场比赛ID", vj.getString("id"));
            nr.put("开赛时间", vj.getString("date") + " " + vj.getString("time"));
            nr.put("客队", vj.getString("a_cn_abbr"));
            nr.put("主队", vj.getString("h_cn"));
            nr.put("主队ID", vj.getString("h_id_dc"));
            nr.put("客队ID", vj.getString("a_id_dc"));
            nr.put("销售状态", vj.getString("status"));
            String ttg = vj.getString("ttg");
            if (StrUtil.isNotBlank(ttg)) {
                JSONObject jqs = new JSONObject();
                JSONObject ttgj = JSONObject.parseObject(ttg);
                jqs.put("0球", ttgj.getString("s0"));
                jqs.put("1球", ttgj.getString("s1"));
                jqs.put("2球", ttgj.getString("s2"));
                jqs.put("3球", ttgj.getString("s3"));
                jqs.put("4球", ttgj.getString("s4"));
                jqs.put("5球", ttgj.getString("s5"));
                jqs.put("6球", ttgj.getString("s6"));
                jqs.put("7+球", ttgj.getString("s7"));
                jqs.put("是否单", ttgj.getString("single"));
                jqs.put("销售状态", ttgj.getString("p_status"));
                nr.put("赔率", jqs.toJSONString());
            }
            array.add(nr);
        }
        System.out.println(JSONObject.toJSONString(array));
    }

    /**
     * 半全场胜负平
     * @throws Exception
     */
    public static void getBQCSF() throws Exception {
        String result = HttpUtil.get("https://i.sporttery.cn/odds_calculator/get_odds?i_format=json&i_callback=getData&poolcode[]=hafu&_=" + DateUtil.date().getTime());
        if (StrUtil.isNotBlank(result)) {
            result = result.replaceAll("getData\\(", "");
            result = result.replaceAll("\\);", "");
        }
        JSONObject json = JSONObject.parseObject(result);
        json.remove("status");
        String datas = json.getString("data");
        JSONObject j = JSONObject.parseObject(datas);
        Set<String> s = j.keySet();
        JSONArray array = new JSONArray();
        for (String key : s) {
            JSONObject nr = new JSONObject();
            String values = j.getString(key);
            JSONObject vj = JSONObject.parseObject(values);
            nr.put("赛事编号", vj.getString("num"));
            nr.put("赛事", vj.getString("l_cn_abbr"));
            nr.put("本场比赛ID", vj.getString("id"));
            nr.put("开赛时间", vj.getString("date") + " " + vj.getString("time"));
            nr.put("客队", vj.getString("a_cn_abbr"));
            nr.put("主队", vj.getString("h_cn"));
            nr.put("主队ID", vj.getString("h_id_dc"));
            nr.put("客队ID", vj.getString("a_id_dc"));
            nr.put("销售状态", vj.getString("status"));
            String hafu = vj.getString("hafu");
            if (StrUtil.isNotBlank(hafu)) {
                JSONObject bqc = new JSONObject();
                JSONObject ha = JSONObject.parseObject(hafu);
                bqc.put("胜胜", ha.getString("hh"));
                bqc.put("胜平", ha.getString("hd"));
                bqc.put("胜负", ha.getString("ha"));
                bqc.put("平胜", ha.getString("dh"));
                bqc.put("平平", ha.getString("dd"));
                bqc.put("平负", ha.getString("da"));
                bqc.put("负胜", ha.getString("ah"));
                bqc.put("负平", ha.getString("ad"));
                bqc.put("负负", ha.getString("aa"));
                bqc.put("状态", ha.getString("p_status"));
                bqc.put("是否单", ha.getString("single"));
                nr.put("赔率", bqc.toJSONString());
            }
            array.add(nr);
        }
        System.out.println(JSONObject.toJSONString(array));
    }

    /**
     * 获取对阵数据
     * @param tid   球队ID
     * @param date  最大时间
     * @param limit 获取条数
     * @throws Exception
     */
    public static void getDuiZhenShuJu(String tid, String date, Integer limit) throws Exception{
        String res = HttpUtil.get("https://i.sporttery.cn/api/fb_match_info/get_team_rec_data?tid="+tid+"&md="+date+"&is_ha=all&limit="+limit+"&c_id=0&ptype[]=three_-1&f_callback=getTeamsDataInfo&_=1594347775919");
        if (StrUtil.isNotBlank(res)) {
            res = res.replaceAll("getTeamsDataInfo\\(", "");
            res = res.replaceAll("\\);", "");
            JSONObject json = JSONObject.parseObject(res);
            json.remove("status");
            String result = json.getString("result");
            if (StrUtil.isNotBlank(result)) {
                JSONObject rej = JSONObject.parseObject(result);
                String datas = rej.getString("data");
                JSONArray dataArr = new JSONArray();
                if (StrUtil.isNotBlank(datas)) {
                    JSONArray array = JSONObject.parseArray(datas);
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject re = new JSONObject();
                        JSONObject data = array.getJSONObject(i);
                        re.put("比赛时间", data.getString("date_cn") + " " + data.getString("time_cn"));
                        re.put("主队名称", data.getString("h_cn"));
                        re.put("主队ID", data.getString("h_id_dc"));
                        re.put("客队名称", data.getString("a_cn"));
                        re.put("客队ID", data.getString("a_id_dc"));
                        re.put("半场比分", data.getString("half"));
                        re.put("全场比分", data.getString("final"));
                        re.put("赛场ID", data.getString("sporttery_matchid"));
                        dataArr.add(re);
                    }
                }
                System.out.println(JSONObject.toJSONString(dataArr));
            }
        }
    }


    /**
     * 获取历史交锋数据
     * @param mid 赛事ID
     * @param limit 查询数量
     * @throws Exception
     */
    public static void getHistoryData(String mid, Integer limit) throws  Exception {
        String res = HttpUtil.get("https://i.sporttery.cn/api/fb_match_info/get_result_his?limit=10&is_ha=all&limit="+limit+"&c_id=0&mid="+mid+"&ptype[]=three_-1&f_callback=getResultHistoryInfo&_=1594347775923");
        if (StrUtil.isNotBlank(res)) {
            res = res.replaceAll("\\);", "");
            res = res.replaceAll("getResultHistoryInfo\\(", "");
            JSONObject resJ = JSONObject.parseObject(res);
            String result = resJ.getString("result");
            if (StrUtil.isNotBlank(result)) {
                JSONArray historyArry = new JSONArray();
                JSONObject resultJson = JSONObject.parseObject(result);
                String datas = resultJson.getString("data");
                if (StrUtil.isNotBlank(datas)) {
                    JSONArray array = JSONObject.parseArray(datas);
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject data = array.getJSONObject(i);
                        JSONObject history = new JSONObject();
                        history.put("赛事ID", data.getString("sporttery_matchid"));
                        history.put("比赛时间", data.getString("date_cn") + " " + data.getString("time_cn"));
                        history.put("主队ID", data.getString("h_id_dc"));
                        history.put("主队名称", data.getString("h_cn"));
                        history.put("客队ID", data.getString("a_id_dc"));
                        history.put("客队名称", data.getString("a_cn"));
                        history.put("半场比分", data.getString("half"));
                        history.put("全场比分", data.getString("final"));
                        historyArry.add(history);
                    }
                }
                System.out.println(JSONObject.toJSONString(historyArry));
            }
        }
    }
}
